<?php
/**
 * Tags Utils: include html tag, javascript tag,...
 * @author PC
 * @author: HoanVX
 * @version $Id: tagUtils.php 48 2015-01-11 09:11:26Z voxuanhoan@gmail.com $
 */
class TagUtils {

	/**
	 * Generate a hidden tag
	 * @param unknown $name
	 * @param unknown $value
	 */
	public static function hidden($name, $value) {
		echo "<input type='hidden' name='$name' value='$value' />";
	}
	
	/**
	 * Generate a <input> html tag with default is hidden tag
	 * @param unknown $args
	 */
	public static function inputTag($args = array()) {
		$default = array(
				'name'	=> '',
				'class'	=> '',
				'value'	=> '',
				'id'	=> '',
				'type'	=> 'hidden'	
		);

		$args = wp_parse_args($args, $default);

		$hiddenTag = '<input type="%1$s" id="%2$s" class="%3$s" value="%4$s" name="%5$s" />';
		echo sprintf($hiddenTag, $args['type'], $args['id'], $args['class'], $args['value'], $args['name']);
	}
	
	/**
	 * Generate a pulldown which is data is pass over options property in args's array
	 * @param unknown array(
	 * 					'options' => 'array('key' => 'value')' //'array value list to put for each option: Required'
	 * 					, 'name'	=> 'Name of select tag : required'
	 * 					, 'id'		=> '' // 'Id of select tag: Defaut blank'
	 * 					, 'class'	=> '' //'class of select tag: Default blank'
	 * 					, 'firstOption=> '' //<option value="">Chọn chuyên mục cần đăng</option>
	 * )
	 * @return select tag
	 */
	public static function genPulldown($args=array()) {
		$default = array(
				'options'		=> array() ,
				'name'			=> '',
				'id'			=> '',
				'class'			=> '',
				'firstOption'	=> '',
				'defaultSelected'=> ''
		);

		$args = wp_parse_args( $args, $default );

		$selectFormat ='<select class="%1$s" id="%2$s" name="%3$s">%4$s</select>';
		$options = $args['firstOption'];
		if (!empty($args['options'])) {

			// Add options
			foreach ($args['options'] as $key => $value) {
				if ($args['defaultSelected'] == $key) 
					$options .= '<option selected value="'. $key .'">'. $value .'</option>';
				else 
					$options .= '<option value="'. $key .'">'. $value .'</option>';
			}
		}
		return sprintf($selectFormat, $args["class"], $args['id'], $args['name'], $options);
	}

	/**
	 * Common category pulldown is used for post.php an header.php page at category items
	 * @param: $args : //default array if parameter is not pass
	 * $args = array(
	 * 			'options'		=> self::$cities ,
	 * 			'name'			=> 'city',
	 * 			'id'			=> 'city',
	 * 			'class'			=> 'form-control',
	 * 			'firstOption'	=> 'Chọn danh mục'
	 * 	);
	 * @return select tag
	 */
	public static function genCategoryPulldown($args = array()) {
		//default array if parameter is not pass (use for select tag)
		$default = array(
				'options'		=> array() ,
				'name'			=> 'category',
				'id'			=> 'category',
				'class'			=> 'form-control',
				'firstOption'	=> 'Chọn danh mục',
				'defaultSelected'=> ''
		);
		$args = wp_parse_args( $args, $default );

		// Get all category
		$catArgs = array(
				'type'                     => '',
				'child_of'                 => 0,
				'parent'                   => '',
				'orderby'                  => 'count',
				'order'                    => 'ASC',
				'hide_empty'               => 0, //1: hide all empty category without posts
				'hierarchical'             => 0, //
				'exclude'                  => '1', //1: Uncategorized
				'include'                  => '',
				'number'                   => '',
				'taxonomy'                 => 'category',
				'pad_counts'               => false
		);
		$cats = get_categories($catArgs) ;

		// Only for 4 category on a row
		$options ='<option value="">'.$args['firstOption']. '</option>';
		foreach ($cats as $cat) {
			// if category is parent
			if($cat->parent == 0) {
				$options .= '<optgroup label="'. $cat->name .'">';
		
				// Filter child categories from parent category
				$argsChild = array(
						'child_of'                 => $cat->term_id,
						'orderby'                  => 'name',
						'order'                    => 'ASC',
						'hide_empty'               => 0, //1: hide all empty category without posts
						'exclude'                  => '1', //1: Uncategorized
						'taxonomy'                 => 'category',
				);
				
				// Get all child category which have parent category has determined
				$catsChilds = get_categories($argsChild);
				foreach ($catsChilds as $catChild) {
					if ($catChild->cat_ID == $args['defaultSelected'])
						$options .= '<option selected value="'. $catChild->cat_ID .'">'. $catChild->name .'</option>';
					else
						$options .= '<option value="'. $catChild->cat_ID .'">'. $catChild->name .'</option>';
				}
				$options .= "</optgroup>";
			}
		}
		$selectFormat = '<select class="%1$s" id="%2$s" name="%3$s">%4$s</select>';

		return sprintf($selectFormat, $args['class'], $args['id'], $args['name'], $options);
	}

	public static function genMessagePanel($message) {
		$panelFormat = '<div class="panel panel-info">'
							.'<div class="panel-heading">%1$s</div>'
      				   .'</div>';
		return sprintf($panelFormat, $message);
	}

	/**
	 * Generate image tag for html
	 * @param unknown $args
	 * @return img tag
	 */
	public static function img($args= array()) {
		$default = array(
				'src'			=> TEMPLATE_DIRECTORY_URI .'/images/no_image.jpg' ,
				'name'			=> '',
				'id'			=> '',
				'class'			=> '',
				'height'		=> '',
				'width'			=> '',
				'alt'			=> ''
		);

		// Merger $args into $default
		$args = wp_parse_args($args, $default);
		if (empty($args['src'])) 
			$args['src'] = TEMPLATE_DIRECTORY_URI .'/images/no_image.jpg';

		$imgFormat = '<img id="%1$s" class="%2$s" name="%3$s" src="%4$s" height="%5$d" width="%6$d" alt="%7$s"/>';
		return sprintf($imgFormat, $args['id'], $args['class'], $args['name'], $args['src'], $args['height'], $args['width'], $args['alt']);
	}

}